package cn.com.flever.component;

import cn.com.flever.utils.GenerateUtils;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.event.*;

public class GenerateUnitCode extends JDialog {
    private JPanel contentPane;
    private JButton generate;
    private JFormattedTextField textField1;
    private Project project;//当前idea的project
    private VirtualFile currentFile;//当前选择的文件目录
    private String path = "";//代码生成路径
    private String packagePath = "";//生成代码在project中的包路径

    public GenerateUnitCode(Project project) {
        //init
        this.project = project;
        if (StringUtils.isEmpty(packagePath)) {
            //目录格式化
            packagePath = project.getBasePath() + "/src/";
            this.textField1.setText(packagePath);
        }
        //设置界面
        setContentPane(contentPane);
        setModal(true);
        setTitle("Generator Unit Code");

        //generate按钮生成代码监听
        generate.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });


        // X关闭按钮事件
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // 键盘Esc关闭事件
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        // add your code here
        GenerateUtils.downloadForGit(packagePath);
        dispose();
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }


    /**
     * 唤出fileChoose
     *
     * @param project       当前所在项目
     * @param chooseFile    是否选择文件
     * @param chooseFolder  是否选择目录
     * @param currentFolder 当前鼠标选择的目录
     * @return
     */
    public static String selectPath(Project project, boolean chooseFile, boolean chooseFolder, VirtualFile currentFolder) {
        //生成fileChoose
        FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(chooseFile, chooseFolder, false, false, false, false);
        //设置当前目录
        VirtualFile selectFile = project.getBaseDir();
        //获取选择的文件
        VirtualFile file = FileChooser.chooseFile(fileChooserDescriptor, project, currentFolder == null ? selectFile : currentFolder);
        return file.getPath();
    }

    public void setTextField1(JFormattedTextField textField1) {
        this.textField1 = textField1;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public void setCurrentFile(VirtualFile currentFile) {
        this.currentFile = currentFile;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setPackagePath(String packagePath) {
        this.packagePath = packagePath;
        this.textField1.setText(packagePath);
    }
}
